package com.itheima.stream.controller;

import com.itheima.stream.config.DelaySource;
import com.itheima.stream.config.MySource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ProducerController {

    @Autowired
    private Source source;
    @Autowired
    private MySource mySource;


    @GetMapping("send")
    public String send(String message) {
        Message<String> mqMsg = MessageBuilder
                .withPayload(message)
                .build();
        source.output().send(mqMsg);
        return "OK";
    }

    @GetMapping("mysend")
    public String mysend(String message) {
 /*       Message<String> mqMsg = MessageBuilder
                .withPayload(message) //消息正文
                .setHeader("name","itheima") // 请求头
                .build();*/

        // Map
        for (int i = 1; i < 16; i++) {
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("id", "1"+i);
            paramMap.put("name", "上海传智教育"+i);
            paramMap.put("addr", "浦东航头万香国际");

            Message< Map<String, String> > mqMsg = MessageBuilder
                    .withPayload(paramMap) //消息正文
                    .setHeader("name","itheima") // 请求头
                    .setHeader("pkey",i) // 请求头
                    .build();

            mySource.output().send(mqMsg);
        }
        return "OK";
    }

    @Autowired
    private DelaySource delaySource;


    @GetMapping("delay_send")
    public String delaySend(String message) {
        Message<String> mqMsg = MessageBuilder
                .withPayload(message)
                //延迟数据配置 5s后消费方接收消息
                .setHeader("x-delay", 10000)
                .build();
        delaySource.output().send(mqMsg);
        System.out.println(new Date());
        return "OK";
    }

}
